$(function() {
	(function() {
		var logisticsTem = {
			'dataE': [], //前台数据
			'dataN': [], //后台数据
			'dataNow': [], //编辑的数据
			'wlcdtion': 0, //0按件,1按重
			'wlcdNum': 0, //全局弹出次数
			'wldis': 0, //0全局，1单个编辑
			'wlallTr': 0, //前台全部tr
			'wlob': $('#logistics-temp-td > b'), //默认运费
			'oshow': $('#logistics-diy'), //指定地区
			'opopDiv': $('#logistics-popup'), //弹出DIV
			'dtinput': $('#logistics-popup').find('dt > input[type="checkbox"]'),
			'ainput': $('#logistics-popup').find('dd > input[type="checkbox"]'),
			'wlaj': $('input.logistics-temp-senda'),
			'wlaz': $('input.logistics-temp-sendb'),
			'wltg': $('#logistics-temp-aa'),
			'wltb': $('#logistics-popup-yes'),
			'wlce': $('#logistics-popup-close'),
			'wlcl': $('#logistics-popup-cancel'),
			'wlcobj': null,
			'wlfile': ''
		};
		/*show*/
		function wlPopWidth() {
				var ow = logisticsTem.opopDiv.outerWidth(true),
					w = $(window).width();
				logisticsTem.opopDiv.css('left', (w - ow) / 2 + 'px').show();
			}
			/*init*/
		function wlcountFn() {
				logisticsTem.oshow.empty();
				logisticsTem.dataE = [];
				logisticsTem.dataN = [];
				logisticsTem.dataNow = [];
				logisticsTem.wlcdNum = 0;
				logisticsTem.wldis = 0;
				logisticsTem.wlallTr = 0;
				logisticsTem.wlcobj = null;
			}
			/*按件计*/
		logisticsTem.wlaj.bind('click', function() {
			var oc = $('<input type="text" name="sendjs" class="logistics-t-ib b-radius" value="1" /> 件内, <input type="text" name="sendy" class="logistics-t-ib b-radius" /> 元 , 每增加 <input type="text" name="sendj" class="logistics-t-ib b-radius" value="1" /> 件，增加运费 <input type="text" name="sendyt" class="logistics-t-ib b-radius" />');
			if (window.confirm('更换计价方式会清除之前设置的运费信息确定更换么?')) {
				logisticsTem.wlcdtion = 0;
				logisticsTem.wlob.empty().append(oc);
				wlcountFn();
			}
		});
		/*按重量计*/
		logisticsTem.wlaz.bind('click', function() {
			var oc = $('<input type="text" name="sendjs" class="logistics-t-ib b-radius" value="1" /> 公斤内，<input type="text" name="sendy" class="logistics-t-ib b-radius" /> 元 ，每增加 <input type="text" name="sendj" class="logistics-t-ib b-radius" value="1" /> 公斤，增加运费 <input type="text" name="sendyt" class="logistics-t-ib b-radius" />');
			if (window.confirm('更换计价方式会清除之前设置的运费信息确定更换么?')) {
				logisticsTem.wlcdtion = 1;
				logisticsTem.wlob.empty().append(oc);
				wlcountFn();
			}
		});
		/*全局弹出*/
		logisticsTem.wltg.bind('click', function() {
			logisticsTem.wldis = 0;
			logisticsTem.wlcdNum++;
			logisticsTem.dataE = [];
			logisticsTem.wlallTr = logisticsTem.oshow.find('.logistics-table > tbody > tr'); //allTr
			var temp = [],
				str = "",
				i, len;
			if (logisticsTem.wlcdNum == 1) {
				logisticsTem.wlcdNum = logisticsTem.wlallTr.length;
			}
			if (logisticsTem.wlallTr.length > 0) {
				logisticsTem.wlallTr.each(function(i) {
					if (i > 0) {
						var va = $(this).children('td:eq(0)').children('input:eq(0)').val();
						temp.push(va);
					}
				});
				str = temp.join(',');
				logisticsTem.dataE = str.split(',');
			}
			logisticsTem.dtinput.attr('checked',false);
			logisticsTem.ainput.each(function(i) {
				$(this).siblings('label').css('color', '#000');
				$(this).attr({
					'disabled': false,
					'checked': false
				});
				var vl = $(this).siblings('label').html();
				for (i = 0, len = logisticsTem.dataE.length; i < len; i++) {
					if (vl == logisticsTem.dataE[i]) {
						$(this).siblings('label').css('color', 'red');
						$(this).attr('disabled', true);
					}
				}
			});
			wl.swloverlay();
			wlPopWidth();
		});
		/*单个编辑*/
		logisticsTem.oshow.bind('click', function(event) {
			logisticsTem.wldis = 1;
			logisticsTem.wlcobj = $(event.target);
			if (logisticsTem.wlcobj.attr('class') == 'b-radius logistsub') {
				logisticsTem.dataE = [];
				logisticsTem.dataNow = [];
				var str = "",
					i, len, j, m,
					temp = [];
				logisticsTem.wlallTr = logisticsTem.oshow.find('.logistics-table > tbody > tr');
				var valu = logisticsTem.wlcobj.closest('tr').children('td:eq(0)').children('input:eq(0)').val();
				logisticsTem.wlallTr.each(function(k) {
					if (k > 0) {
						var vl = $(this).children('td:eq(0)').children('input:eq(0)').val();
						temp.push(vl);
					}
				});
				str = temp.join(',');
				logisticsTem.dataE = str.split(',');
				logisticsTem.dataNow = valu.split(',');
				logisticsTem.dtinput.attr('checked',false);
				logisticsTem.ainput.each(function(i) {
					$(this).siblings('label').css('color', '#000');
					$(this).attr({
						'disabled': false,
						'checked': false
					});
					var vl = $(this).siblings('label').html();
					for (i = 0, len = logisticsTem.dataE.length; i < len; i++) {
						if (vl == logisticsTem.dataE[i]) {
							$(this).siblings('label').css('color', 'red');
							$(this).attr('disabled', true);
						}
					}
					for (j = 0, m = logisticsTem.dataNow.length; j < m; j++) {
						if (vl == logisticsTem.dataNow[j]) {
							$(this).siblings('label').css('color', '#000');
							$(this).attr('disabled', false);
							$(this).attr('checked', true);
						}
					}
				});
				wl.swloverlay();
				wlPopWidth();
			} else if (logisticsTem.wlcobj.attr('class') == 'b-radius logistdel') {
				if (window.confirm('确定删除吗？')) {
					logisticsTem.wlcobj.closest('tr').remove();
				}
			}
		});
		/*保存*/
		logisticsTem.wltb.bind('click', function() {
			logisticsTem.dataN = [];
			var hidStr = [];
			if (logisticsTem.wldis === 0) {
				logisticsTem.ainput.each(function() {
					if ($(this).attr('checked') && $(this).attr('disabled') == false) {
						var v = $(this).siblings('label').html();
						hidStr.push($(this).attr('id'));
						logisticsTem.dataN.push(v);
					}
				});
				var str = logisticsTem.dataN.join(',');
				logisticsTem.wlfile = hidStr.join(',');

				if (logisticsTem.wlallTr.length > 0) {
					if (logisticsTem.wlcdtion === 0 && str != '') {
						var otablea = $('<tr><td><input type="text" class="logistics-t-it" value="' + str + '" /><input type="hidden" name="logistics[' + logisticsTem.wlcdNum + '][1]" value="' + logisticsTem.wlfile + '" /></td><td><input type="text" name="logistics[' + logisticsTem.wlcdNum + '][2]" value="1" /></td><td><input type="text" name="logistics[' + logisticsTem.wlcdNum + '][3]" /></td><td><input type="text" name="logistics[' + logisticsTem.wlcdNum + '][4]" value="1" /></td><td><input type="text" name="logistics[' + logisticsTem.wlcdNum + '][5]" /></td><td><ul><li><a class="b-radius logistsub" data-val="sub">编辑</a></li><li><a class="b-radius logistdel" data-val="del">删除</a></li></ul></td></tr>');
						logisticsTem.oshow.find('.logistics-table > tbody').append(otablea);
					} else if (logisticsTem.wlcdtion === 1 && str != '') {
						var otableb = $('<tr><td><input type="text" class="logistics-t-it" value="' + str + '" /><input type="hidden" name="logistics[' + logisticsTem.wlcdNum + '][1]" value="' + logisticsTem.wlfile + '" /></td><td><input type="text" name="logistics[' + logisticsTem.wlcdNum + '][2]" value="1" /></td><td><input type="text" name="logistics[' + logisticsTem.wlcdNum + '][3]" /></td><td><input type="text" name="logistics[' + logisticsTem.wlcdNum + '][4]" value="1" /></td><td><input type="text" name="logistics[' + logisticsTem.wlcdNum + '][5]" /></td><td><ul><li><a class="b-radius logistsub" data-val="sub">编辑</a></li><li><a class="b-radius logistdel" data-val="del">删除</a></li></ul></td></tr>');
						logisticsTem.oshow.find('.logistics-table > tbody').append(otableb);
					}
				} else {
					if (logisticsTem.wlcdtion === 0 && str != '') {
						var otablea = $('<td>&nbsp;</td><td><table width="99%" class="logistics-table"><tr><th>运送到</th><th>首件（件）</th><th>首费（元）</th><th>续件（件）</th><th>续费（元）</th><th width="100">操作</th></tr><tr><td><input type="text" class="logistics-t-it" value="' + str + '" /><input type="hidden" name="logistics[1][1]" value="' + logisticsTem.wlfile + '" /></td><td><input type="text" name="logistics[1][2]" value="1" /></td><td><input type="text" name="logistics[1][3]" /></td><td><input type="text" name="logistics[1][4]" value="1" /></td><td><input type="text" name="logistics[1][5]" /></td><td><ul><li><a class="b-radius logistsub" data-val="sub">编辑</a></li><li><a class="b-radius logistdel" data-val="del">删除</a></li></ul></td></tr></table></td>');
						logisticsTem.oshow.append(otablea);
					} else if (logisticsTem.wlcdtion === 1 && str != '') {
						var otableb = $('<td>&nbsp;</td><td><table width="99%" class="logistics-table"><tr><th>运送到</th><th>首重（公斤）</th><th>首费（元）</th><th>续重（公斤）</th><th>续费（元）</th><th width="100">操作</th></tr><tr><td><input type="text" class="logistics-t-it" value="' + str + '" /><input type="hidden" name="logistics[1][1]" value="' + logisticsTem.wlfile + '" /></td><td><input type="text" name="logistics[1][2]" value="1" /></td><td><input type="text" name="logistics[1][3]" /></td><td><input type="text" name="logistics[1][4]" value="1" /></td><td><input type="text" name="logistics[1][5]" /></td><td><ul><li><a class="b-radius logistsub" data-val="sub">编辑</a></li><li><a class="b-radius logistdel" data-val="del">删除</a></li></ul></td></tr></table></td>');
						logisticsTem.oshow.append(otableb);
					}
				}
			} else if (logisticsTem.wldis === 1) {
				logisticsTem.ainput.each(function() {
					if ($(this).attr('checked') && $(this).attr('disabled') == false && $(this).siblings('label').css('color') != 'red') {
						var v = $(this).siblings('label').html();
						logisticsTem.dataN.push(v);
						hidStr.push($(this).attr('id'));
					}
				});
				var str = logisticsTem.dataN.join(',');
				logisticsTem.wlfile = hidStr.join(',');
				if (str != '') {
					logisticsTem.wlcobj.closest('tr').children('td:eq(0)').children('input:eq(0)').val(str);
					logisticsTem.wlcobj.closest('tr').children('td:eq(0)').children('input:eq(1)').val(logisticsTem.wlfile);
				}else{
					logisticsTem.wlcobj.closest('tr').remove();
				}
			}
			wl.hwloverlay();
			logisticsTem.opopDiv.hide();
		});
		/*关闭*/
		logisticsTem.wlce.bind('click', function() {
			if (logisticsTem.wldis == 0) {
				logisticsTem.wlcdNum--;
			}
			wl.hwloverlay();
			logisticsTem.opopDiv.hide();
		});
		/*取消*/
		logisticsTem.wlcl.bind('click', function() {
			if (logisticsTem.wldis == 0) {
				logisticsTem.wlcdNum--;
			}
			wl.hwloverlay();
			logisticsTem.opopDiv.hide();
		});

	})();

	function logistics_vd() {
		var ai = document.logistics_frm.elements;
		var i, len, bool = true,
			resultAll = [];
		var ra = /^\d+$|^[0-9]+\.\d{1,2}$/;
		var rb = /^$/;
		for (i = 0, len = ai.length; i < len; i++) {
			if (ai[i].type === 'text' && ai[i].name != 'tempname' && !ai[i].className) {
				var result = [];
				if (rb.test(ai[i].value)) {
					result[0] = false;
					result[1] = ai[i];
					result[2] = "不能为空";
					resultAll.push(result);
				} else if (!ra.test(ai[i].value)) {
					result[0] = false;
					result[1] = ai[i];
					result[2] = "格式错误";
					resultAll.push(result);
				}
			}
		}
		return resultAll;
	}
	$('form[name="logistics_frm"]').submit(function() {
		var res = logistics_vd();
		var i, len;
		var ra = /^\d+$|^[0-9]+\.\d{1,2}$/;
		var rb = /^$/;
		if (res.length < 1) {
			return true;
		} else {
			for (i = 0, len = res.length; i < len; i++) {
				if ($(res[i][1]).siblings('label')) {
					$(res[i][1]).siblings('label').remove();
					$(res[i][1]).siblings('br').remove();
				}
				$(res[i][1]).after('<br/><label style="color:red">' + res[i][2] + '</label>');
				$(res[i][1]).bind('blur', function() {
					var v = $(this).val();
					if (rb.test(v)) {
						$(this).siblings('label').html('不能为空');
					} else if (!ra.test(v)) {
						$(this).siblings('label').html('格式错误');
					} else {
						$(this).siblings('label').html('');
					}
				});
			}
			return false;
		}
	});
	if ($('#pop_check_wrap').length > 0) {
		$('#pop_check_wrap').bind('click', function(event) {
			var $target = $(event.target);
			if ($target.is('.reg')) {
				var $checked = $target.attr('checked');
				var $all_checkbox = $target.parent().nextAll('dd').children('input[type="checkbox"]');
				$.each($all_checkbox, function() {
					if (!$(this).attr('disabled')) {
						$(this).attr('checked', $checked);
					}
				});
			}
		});
	}
});